package cn.iocoder.yudao.module.biz.service.risk.basicManage;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.risk.basicManage.vo.*;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskInfo.vo.BizRiskInfoRespVO;
import cn.iocoder.yudao.module.biz.convert.risk.basicManage.BizBasicManageConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.basicManage.BizBasicManageDO;
import cn.iocoder.yudao.module.biz.dal.mysql.risk.basicManage.BizBasicManageMapper;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO;
import cn.iocoder.yudao.module.system.convert.user.UserConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_IMPORT_LIST_IS_EMPTY;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_USERNAME_EXISTS;

/**
 * 基础管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizBasicManageServiceImpl implements BizBasicManageService {

    @Resource
    private BizBasicManageMapper basicManageMapper;
    @Resource
    private DeptMapper deptMapper;

    @Override
    public String createBasicManage(BizBasicManageCreateReqVO createReqVO) {
        // 插入
        BizBasicManageDO basicManage = BizBasicManageConvert.INSTANCE.convert(createReqVO);
        basicManage.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        basicManageMapper.insert(basicManage);
        // 返回
        return basicManage.getId();
    }

    @Override
    public void updateBasicManage(BizBasicManageUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateBasicManageExists(updateReqVO.getId());
        // 更新
        BizBasicManageDO updateObj = BizBasicManageConvert.INSTANCE.convert(updateReqVO);
        basicManageMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicManage(String id) {
        // 校验存在
        this.validateBasicManageExists(id);
        // 删除
        basicManageMapper.deleteById(id);
    }

    private void validateBasicManageExists(String id) {
        if (basicManageMapper.selectById(id) == null) {
            throw exception(BASIC_MANAGE_NOT_EXISTS);
        }
    }

    @Override
    public BizBasicManageDO getBasicManage(String id) {
        return basicManageMapper.selectById(id);
    }

    @Override
    public List<BizBasicManageDO> getBasicManageList(Collection<String> ids) {
        return basicManageMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizBasicManageDO> getBasicManagePage(BizBasicManagePageReqVO pageReqVO) {
        pageReqVO.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        return basicManageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizBasicManageDO> getBasicManageList(BizBasicManageExportReqVO exportReqVO) {
        exportReqVO.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        return basicManageMapper.selectList(exportReqVO);
    }

    @Override
    public List<BizBasicManageRespVO> getBasicPage(BizBasicManagePageReqVO pageReqVO) {
        Integer pageNo=pageReqVO.getPageNo();
        Integer pageSize=  pageReqVO.getPageSize();
        //设置为limit开始的条数
        pageReqVO.setPageNo((pageNo-1)*pageSize);
        pageReqVO.setDeptId(SecurityFrameworkUtils.getLoginUser().getDeptId()+"");
        return basicManageMapper.getBasicPage(pageReqVO);
    }

    @Override
    public Integer getBasicCount(BizBasicManagePageReqVO pageReqVO) {
        return basicManageMapper.getBasicCount(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务，异常则回滚所有导入
    public BizBasicManageImportRespVO importExcel(List<BizBasicManageExcelVO> importBasic) {
        if (CollUtil.isEmpty(importBasic)) {
            throw exception(BASIC_MANAGE_LIST_IS_EMPTY);
        }
        BizBasicManageImportRespVO respVO = BizBasicManageImportRespVO.builder().failureBasicnames(new LinkedHashMap<>()).build();
        Map<String,String> map = new HashMap<>();
        for(BizBasicManageExcelVO vo : importBasic){
            //判断实施单位是否存在
            DeptDO deptDO = deptMapper.selectOne(new QueryWrapperX<DeptDO>().eqIfPresent("name",vo.getExplotUnit()));
            if(deptDO == null){//如果实施单位不存在
                map.put(vo.getBasicName()+"的实施单位","不存在，请手动修改");
                vo.setExplotUnit(null);
                respVO.setFailureBasicnames(map);
            }else{
                vo.setExplotUnit(String.valueOf(deptDO.getId()));
            }
            BizBasicManageCreateReqVO basicManageCreateReqVO = new BizBasicManageCreateReqVO();
            basicManageCreateReqVO.setBasicName(vo.getBasicName());
            basicManageCreateReqVO.setBasicContent(vo.getBasicContent());
            basicManageCreateReqVO.setBasicArea(vo.getBasicArea());
            basicManageCreateReqVO.setExplotUnit(vo.getExplotUnit());
            createBasicManage(basicManageCreateReqVO);
        }
        return respVO;
    }

    @Override
    public List<BizBasicManageDO> getBasicForExecl(BizBasicManageExportReqVO exportReqVO) {
        exportReqVO.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        return basicManageMapper.getBasicForExecl(exportReqVO);
    }
}
